package com.contactsconverter.service;



import com.contactsconverter.activity.CTLoginActivity;
import com.contactsconverter.utils.CTUtils;
import com.contactsconverter.R;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class AccountAuthenticator extends AbstractAccountAuthenticator{

	private Context mContext;
	public AccountAuthenticator(Context context) {
		super(context);
		this.mContext = context;
	}

	@Override
	public Bundle addAccount(AccountAuthenticatorResponse response,
			String accountType, String authTokenType, String[] requiredFeatures,
			Bundle options) {

		Bundle reply = new Bundle();

		if(accountExists()){//only one account is allowed as of now
			reply.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_BAD_REQUEST);
			reply.putString(AccountManager.KEY_ERROR_MESSAGE,
					this.mContext.getResources().getString(R.string.only_one_account).toString());
			response.onResult(reply);
			return null;
		}

		final Intent intent = new Intent(mContext, CTLoginActivity.class);
		intent.putExtra(CTUtils.PARAM_AUTHTOKEN_TYPE,
				authTokenType);
		intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
				response);
		final Bundle bundle = new Bundle();
		bundle.putParcelable(AccountManager.KEY_INTENT, intent);
		return bundle;
	}
	
	@Override
	public Bundle confirmCredentials(AccountAuthenticatorResponse response,
			Account account, Bundle arg2) throws NetworkErrorException {
		final Intent intent = new Intent(mContext, CTLoginActivity.class);
        intent.putExtra(CTUtils.PARAM_USERNAME, account.name);
        intent.putExtra(CTUtils.PARAM_CONFIRMCREDENTIALS, true);
        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
        final Bundle bundle = new Bundle();
        bundle.putParcelable(AccountManager.KEY_INTENT, intent);
        return bundle;
	}

	@Override
	public Bundle editProperties(AccountAuthenticatorResponse arg0, String arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Bundle getAuthToken(AccountAuthenticatorResponse arg0, Account arg1,
			String arg2, Bundle arg3) throws NetworkErrorException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAuthTokenLabel(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	 @Override
     public Bundle hasFeatures(AccountAuthenticatorResponse response,
         Account account, String[] features) {
         final Bundle result = new Bundle();
         result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
         return result;
     }

	

	@Override
	public Bundle updateCredentials(AccountAuthenticatorResponse arg0,
			Account arg1, String arg2, Bundle arg3)
					throws NetworkErrorException {
		// TODO Auto-generated method stub
		return null;
	}

	private boolean accountExists(){
		AccountManager am = AccountManager.get(mContext);
		Account[] acts = am.getAccountsByType(CTUtils.ACCOUNT_TYPE);
		return acts.length > 0;
	}
}
